package com.gh.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gh.reggie.common.R;
import com.gh.reggie.dto.DishDto;
import com.gh.reggie.dto.SetmealDto;
import com.gh.reggie.entity.Category;
import com.gh.reggie.entity.Setmeal;
import com.gh.reggie.service.CategoryService;
import com.gh.reggie.service.SetmealDishService;
import com.gh.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * 保存套餐和套餐中的菜品
     * 数据表（setmeal，setmealDish）
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save (@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());

        setmealService.saveWithDish(setmealDto);
        return R.success("保存套餐成功！");
    }


    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //构建分页查询器
        Page<Setmeal> pageInfo= new Page<>(page,pageSize);
        Page<SetmealDto> withNameInfo = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);


        setmealService.page(pageInfo,queryWrapper);

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list  = records.stream().map((item)->{
            //创建一个对象接收套餐信息，添加套餐名
            SetmealDto setmealDto =new SetmealDto();
            //复制套餐信息到套餐传输对象
            BeanUtils.copyProperties(item,setmealDto);
            //获取对象中套餐id
            Long categoryId = setmealDto.getCategoryId();
            //根据id查询对应的名字
            Category category = categoryService.getById(categoryId);
            //没查到直接返回数据，查到就对应赋值
            if(category != null){
                setmealDto.setCategoryName(category.getName());

            }
            return setmealDto;
        }).collect(Collectors.toList());
        BeanUtils.copyProperties(pageInfo,withNameInfo,"records");
        withNameInfo.setRecords(list);

        return R.success(withNameInfo);
    }

    /**
     * 删除套餐（批量删除）
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> id){
        log.info(id.toString());

        setmealService.removeWithDish(id);
        return R.success("删除套餐成功！");
    }


    /**
     * 套餐售卖状态单条变更和批量变更
     * @param param
     * @param id
     * @return
     */
    @PostMapping("/status/{param}")
    public R<String> updateStatus(@PathVariable Integer param,@RequestParam List<Long> id){
        log.info(String.valueOf(param));
        log.info(id.toString());
        setmealService.lambdaUpdate().set(Setmeal::getStatus,param).in(Setmeal::getId,id).update();
        
        return R.success("套餐状态变更成功");
    }


    /**
     * 根据id查寻录入的菜品信息回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> melDtoById(@PathVariable Long id){
        log.info(id.toString());

        SetmealDto setmealDto = setmealService.getMealDtoById(id);

        return R.success(setmealDto);
    }


    @PutMapping
    public R<String> update (@RequestBody SetmealDto setmealDto){
        log.info(setmealDto.toString());

        setmealService.updateWithDish(setmealDto);

        return R.success("更新套餐成功");
    }


    /**
     * 手机端根据条件查询数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //@RequestBody一般用于json形式数据接收
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() !=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }


}
